/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using log4net;
using OpenServers.Net.FtpServer.State;

namespace OpenServers.Net.FtpServer.Command {
    /// <summary>
    ///     <para>
    ///         <b>RESTART (REST)</b><br/>
    ///         The argument field represents the server marker at which
    ///         file transfer is to be restarted.  This command does not
    ///         cause file transfer but skips over the file to the specified
    ///         data checkpoint.  This command shall be immediately followed
    ///         by the appropriate FTP service command which shall cause
    ///         file transfer to resume.
    ///     </para>
    ///     <para>
    ///         <b>Format</b><br/>
    ///         <code>REST &lt;marker&gt; &lt;CRLF&gt;
    /// &lt;marker&gt; ::= &lt;pr-string&gt;
    /// &lt;pr-string&gt; ::= &lt;pr-char&gt; | &lt;pr-char&gt;&lt;pr-string&gt;
    /// &lt;pr-char&gt; ::= printable characters, any
    ///               ASCII code 33 through 126
    /// </code>
    ///     </para>
    ///     <para>
    ///         <b>Success:</b>
    ///         <list type="bullet">
    ///             <item><term>350</term><description>Requested file action pending further information.</description></item>
    ///         </list>
    ///     </para>
    ///     <para>
    ///         <b>Errors:</b>
    ///         <list type="bullet">
    ///             <item><term>421</term><description>Service not available, closing control connection.</description></item>
    ///             <item><term>500</term><description>Syntax error, command unrecognized.</description></item>
    ///             <item><term>501</term><description>Syntax error in parameters or arguments.</description></item>
    ///             <item><term>502</term><description>Command not implemented, superfluous at this site.</description></item>
    ///             <item><term>530</term><description>Not logged in.</description></item>
    ///         </list>
    ///     </para>
    /// </summary>
    public class RestartCommand : BaseCommand {
        private static readonly ILog log = LogManager.GetLogger(typeof(RestartCommand));

        /// <summary>
        /// Sets the restart data in the state.
        /// </summary>
        public override void PerformCommand(IFtpServer server, FtpState state, string arguments) {
            if (string.IsNullOrEmpty(arguments)) {
                log.WarnFormat("invalid arguments for REST command [{0}]", arguments);
                state.Listener.Send(state, server.Messages.Get501());
            } else {
                state.LastRestart = arguments;
                state.Listener.Send(state, server.Messages.Get350());
            }
        }
    }
}
